<template>
  <div class="app-container">
    <!-- 对话框(添加 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="故障类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择故障类型">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DEVICE_FAULT_CATEGORY)"
                       :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
          </el-select>
        </el-form-item>
        <el-form-item label="故障等级" prop="grade">
          <el-select v-model="form.grade" placeholder="请选择故障等级">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DEVICE_FAULT_GRADE)"
                       :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
          </el-select>
        </el-form-item>
        <el-form-item label="故障原因" prop="reason">
          <el-input v-model="form.reason" type="textarea" autosize placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="处理方法" prop="handleMethod">
          <el-input v-model="form.handleMethod" type="textarea" autosize placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createFaultHandle } from "@/api/device/faultHandle";

export default {
  name: "FaultHandle",
  components: {
  },
  props: {
    id: {
      type: Number,
      required: true
    },
    isOpen: {
      type: Boolean,
      require: true,
      default: false
    }
  },
  watch: {
    isOpen: {
      handler(val) {
        this.isOpen = val;
        this.open = val
      }
    },
    id: {
      handler(val) {
        this.id = val;
        this.form.faultId = val
      }
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 弹出层标题
      title: "报修单处理",
      // 是否显示弹出层
      open: this.isOpen,
      // 表单参数
      form: {
        id: undefined,
        faultId: this.id,
        type: undefined,
        grade: undefined,
        reason: undefined,
        handleMethod: undefined
      },
      // 表单校验
      rules: {
        faultId: [{ required: true, message: "报修工单不能为空", trigger: "blur" }],
        type: [{ required: true, message: "故障类型不能为空", trigger: "change" }],
        grade: [{ required: true, message: "故障等级不能为空", trigger: "change" }],
        reason: [{ required: true, message: "故障原因不能为空", trigger: "change" }],
        handleMethod: [{ required: true, message: "处理方法不能为空", trigger: "change" }],
      }
    };
  },
  created() {
  },
  methods: {
    /** 取消按钮 */
    cancel() {
      this.$emit("close")
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 添加的提交
        createFaultHandle(this.form).then(response => {
          this.$modal.msgSuccess("处理成功");
          this.cancel();
        });
      });
    },
  }
};
</script>
